package com.hqyj.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.shop.pojo.Recipient;
import com.hqyj.shop.mapper.RecipientMapper;
import com.hqyj.shop.service.RecipientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.shop.util.JwtUtil;
import com.hqyj.shop.util.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2024-10-15 03:22:38
 */
@Service
public class RecipientServiceImpl extends ServiceImpl<RecipientMapper, Recipient> implements RecipientService {

    @Resource
    RecipientMapper mapper;

    @Override
    public ResultVo addRecipient(Recipient r, HttpServletRequest request) {
        //获取Token
        String token = request.getHeader("Authorization");
        //设置userId
        String userId = JwtUtil.getAudience(token);
        r.setUserId(Integer.parseInt(userId));
        int num = mapper.insert(r);
        if(num > 0 ){
            return ResultVo.success("添加收货人成功");
        }
        return ResultVo.error("添加收货人失败");
    }

    @Override
    public ResultVo queryRecipient(HttpServletRequest request) {
        //获取Token
        String token = request.getHeader("Authorization");
        //设置userId
        String userId = JwtUtil.getAudience(token);
        QueryWrapper<Recipient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Recipient> list = mapper.selectList(queryWrapper);
        if(list.size()>0){
            return ResultVo.success(list);
        }
        return ResultVo.error();
    }
}
